package com.study.opcua.config;

import com.study.opcua.thread.PublicThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

@Configuration
@EnableScheduling
public class DynamicScheduleConfig implements SchedulingConfigurer {

    @Autowired
    private OpcParams opcParams;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        // 线程数
        int threadNum = 10;
        if(opcParams.getThreadNum() != null){
            threadNum = opcParams.getThreadNum();
        }
        // 定时任务间隔时间，默认1s
        long readTime = 1000;
        if(opcParams.getReadTime() != null){
            readTime = opcParams.getReadTime();
        }
        // 创建定时任务线程
        taskRegistrar.addFixedDelayTask(new PublicThread(threadNum, opcParams.getInterfaces()), readTime);
    }
}
